<?php

namespace App\Repositories\Impl;

use App\Models\GovernmentInfo;
use App\Repositories\Contracts\GovernmentInfoRepository;
use App\Repositories\Transformers\GovernmentTopTransformer;
use App\Repositories\Transformers\GovernmentInfoTransformer;
use App\Repositories\Transformers\GovernmentInfoListTransformer;

class GovernmentInfoRepositoryImpl implements GovernmentInfoRepository
{
    public function paginate($perPage, $filter)
    {
        $governmentInfos = GovernmentInfo::orderBy('created_at', 'desc')->paginate($perPage);

        $result = fractal($governmentInfos, new GovernmentInfoListTransformer());

        return $result->toArray();
    }

    public function findTop($count)
    {
        $governmentInfos = GovernmentInfo::take($count)->orderBy("id", "desc")->get();

        $result = fractal($governmentInfos, new GovernmentTopTransformer());

        return $result->toArray();

    }
    public function find($id, $columns = ['*'])
    {
        $governmentInfo = GovernmentInfo::find($id, $columns);

        $result = fractal($governmentInfo, new GovernmentInfoTransformer());

        return $result->toArray();
    }

    public function findByFilter($filter, $columns = ['*'])
    {
        return GovernmentInfo::where($filter)->first($columns);
    }

    public function store($data)
    {
        $governmentInfo = GovernmentInfo::create($data);

        $result = fractal($governmentInfo, new GovernmentInfoTransformer());
        return $result->toArray();
    }

    public function update($data, $id)
    {
        return GovernmentInfo::whereId($id)->updateOrCreate($data);
    }

    public function updateByFilter($data, $where)
    {
        return GovernmentInfo::where($where)->update($data);
    }

    public function delete($id)
    {
        return GovernmentInfo::whereId($id)->delete();
    }
}
